package com.example.webbrowser.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.webbrowser.Entity.MarkListView;
import java.util.ArrayList;
import java.util.List;
import java.util.function.LongFunction;

public class SQLManager extends SQLiteOpenHelper implements bd{
    private static final String DEG_TAG ="webBrowser_SQLManager";
    public static final String TABLE_HISTORY = "BookMark";



    public SQLManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);

    }



    @Override

    public void onCreate(SQLiteDatabase db) {
        //创建表
        String sql="create table BookMark(id integer primary key autoincrement,name text,url text)";
        db.execSQL(sql);
        //Log.d(DEG_TAG,"成功");

    }



    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {

        // Drop older table if it exists
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
        // Create tables again
        onCreate(db);

    }

    public void deleteData(SQLiteDatabase sqLiteDatabase){
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
        String sql="create table BookMark(id integer primary key autoincrement,name text,url text)";
        sqLiteDatabase.execSQL(sql);
        //Log.d(DEG_TAG,"初始化成功");
    }



    @Override
    public boolean addMark(SQLiteDatabase sqLiteDatabase, String name,String url) {
        ContentValues mark = new ContentValues();

        mark.put("name", name);

        mark.put("url", url);

        long id = sqLiteDatabase.insert("BookMark",null, mark);

        if(id!=-1){
            return true;

        }else{
            return false;

        }

    }



    @Override
    public boolean deleteMark(SQLiteDatabase sqLiteDatabase, String id){
        int number = sqLiteDatabase.delete("BookMark","id=?", new String[]{id});

        if(number!=0){
            Log.d(DEG_TAG,id+"删除成功");
            return true;

        }else{
            return false;

        }

    }

    @Override
    public boolean deleteMarkByUrl(SQLiteDatabase sqLiteDatabase, String url) {
        int number = sqLiteDatabase.delete("BookMark","url=?", new String[]{url});

        if(number!=0){
            Log.d(DEG_TAG,url+"删除成功");
            return true;

        }else{
            return false;

        }
    }

    @Override
    public String getId(SQLiteDatabase sqLiteDatabase, String url) {
        //db.query("person_inf",new String[]{"_id,name,age"},"name like ?",new String []{"孙%"},null,null,"personid desc","5,10");
        //Cursor id = sqLiteDatabase.query("BookMark",new String[]{"url"}, "url = ?",url,null,null,null);
        Cursor cursor = sqLiteDatabase.rawQuery("select * from BookMark where url = ?" , new String[]{url});
        //int i =cursor.getColumnIndex("id");
        //Log.d(DEG_TAG,"查询成功，"+i);
        String s=" ";
        if(cursor.moveToFirst()){
            s = cursor.getString(0);
            Log.d(DEG_TAG,"查询成功，"+s);
        }
        cursor.close();
        return s;

    }


    @Override
    public boolean alterMark(SQLiteDatabase sqLiteDatabase, String id,String name, String url) {
        ContentValues mark = new ContentValues();

        mark.put("name", name);

        mark.put("url", url);

        int number = sqLiteDatabase.update("BookMark", mark,"id=?", new String[]{id});

        if(number!=0){
            return true;

        }else{
            return false;

        }

    }



    @Override
    public List<MarkListView> getAllMark(SQLiteDatabase sqLiteDatabase) {
        List<MarkListView> resultMap = new ArrayList<MarkListView>();

        Cursor result = sqLiteDatabase.query("BookMark",null, null,null, null,null, "id");

        while(result.moveToNext()){
            String id = String.valueOf(result.getInt(result.getColumnIndex("id")));

            String name = result.getString(result.getColumnIndex("name"));

            String url = result.getString(result.getColumnIndex("url"));

            //Log.d(DEG_TAG,"id:"+id+",name:"+name+",url:"+url);

            resultMap.add(new MarkListView(id,name,url));

        }

        return resultMap;

    }



    @Override
    public boolean checkUrl(SQLiteDatabase sqLiteDatabase, String url) {
        Cursor result = sqLiteDatabase.query("BookMark",null, "url=?", new String[]{url},null, null,null);

        if(result.getCount()>0){
            return true;

        }else{
            return false;

        }

    }

    @Override
    public void transactionAround(boolean readOnly, CallBack callback) {

    }

}
